import axios, {type AxiosResponse, AxiosError} from "axios";
import { ElMessage } from 'element-plus'

export const request = axios.create({
  baseURL: '/api',
})

request.interceptors.response.use(function (resp: AxiosResponse<Base<any>>): AxiosResponse {
  if (resp.data.code !== 200) {
    ElMessage({
      message: resp.data.msg,
      type: 'error',
      duration: 3000,
      showClose: true,
    })
  }
  return resp
}, (err: AxiosError) => {
  if (err.response) {
    const msg = (err.response.data as Base<any>).msg;
    ElMessage({
      message: msg,
      type: 'error',
      duration: 3000,
      showClose: true,
    })
    return Promise.reject(err.response)
  }
  return Promise.reject(err)
})